<template>
  <div class="jsbos-common-layout">
    <div class="jsbos-common-layout-left" style="width: 80%">
      <div class="jsbos-common-layout-center jsbos-flex-main">
        <el-row class="jsbos-common-search-box" :gutter="16">
          <el-form @submit.native.prevent>
            <el-col :span="4">
              <el-form-item label="关键字">
                <el-input
                  v-model="listQuery.keyword"
                  placeholder="请输入姓名/账号/手机号"
                  clearable
                  @keyup.enter.native="search()"
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item>
                <el-button
                  type="primary"
                  icon="el-icon-search"
                  @click="search()"
                >
                  {{ $t("common.search") }}</el-button
                >
                <el-button icon="el-icon-refresh-right" @click="reset()"
                  >{{ $t("common.reset") }}
                </el-button>
              </el-form-item>
            </el-col>
          </el-form>
        </el-row>
        <div class="jsbos-common-layout-main jsbos-flex-main">
          <jsbos-table
            v-loading="listLoading"
            :data="tableData"
            :highlight-current-row="true"
            @row-click="getMenuList($event, audit)"
          >
            <el-table-column
              prop="account"
              label="账户"
              width="100"
              show-overflow-tooltip
            />
            <el-table-column prop="realName" label="姓名" width="100" />
            <el-table-column
              prop="organize"
              label="所属组织"
              min-width="200"
              show-overflow-tooltip
            />
            <el-table-column
              prop="creatorTime"
              label="创建时间"
              :formatter="jsbos.tableDateFormat"
              width="120"
            />
            <el-table-column
              prop="changePasswordDate"
              label="密码修改时间"
              :formatter="jsbos.tableDateFormat"
              width="120"
            />
            <el-table-column
              prop="lastLogTime"
              label="最后登录时间"
              :formatter="jsbos.tableDateFormat"
              width="120"
            />
            <el-table-column
              prop="enabledMark"
              label="状态"
              width="70"
              align="center"
            >
              <template slot-scope="scope">
                <el-tag
                  type="success"
                  disable-transitions
                  v-if="scope.row.enabledMark == 1"
                  >正常
                </el-tag>
                <el-tag
                  type="warning"
                  disable-transitions
                  v-else-if="scope.row.enabledMark == 2"
                  >锁定
                </el-tag>
                <el-tag type="danger" disable-transitions v-else>禁用</el-tag>
              </template>
            </el-table-column>
          </jsbos-table>
          <pagination
            :total="total"
            :page.sync="listQuery.currentPage"
            :limit.sync="listQuery.pageSize"
            @pagination="initData"
          />
        </div>
      </div>
    </div>

    <div
      class="jsbos-common-layout-main jsbos-flex-main"
      style="width: 20%; background-color: #fff"
    >
      <el-tabs v-model="menuType" class="menu-tab">
        <el-tab-pane label="Web菜单" name="Web"></el-tab-pane>
        <el-tab-pane label="App菜单" name="App"></el-tab-pane>
        <div class="jsbos-common-tree-search-box" style="overflow-y: hidden">
          <el-input
            placeholder="输入关键字"
            v-model="filterText"
            style="width: 50%"
            suffix-icon="el-icon-search"
            clearable
          />
          <el-select
            v-model="audit"
            placeholder="请选择"
            style="margin-left: 5%; width: 30%"
          >
            <el-option
              v-for="(item, index) in auditOptions"
              :key="index"
              :label="item.fullName"
              :value="item.id"
            ></el-option>
          </el-select>

          <el-dropdown style="margin-left: 5%">
            <el-link icon="icon-ym icon-ym-mpMenu" :underline="false" />
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="toggleExpand(true)"
                >展开全部</el-dropdown-item
              >
              <el-dropdown-item @click.native="toggleExpand(false)"
                >折叠全部</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </el-tabs>
      <el-scrollbar
        class="jsbos-common-el-tree-scrollbar"
        v-loading="treeLoading"
      >
        <el-tree
          ref="treeBox"
          :data="treeData"
          :props="defaultProps"
          :default-expand-all="expands"
          highlight-current
          :expand-on-click-node="false"
          node-key="id"
          @node-click="handleNodeClick"
          class="jsbos-common-el-tree"
          v-if="refreshTree"
          :filter-node-method="filterNode"
        >
          <span
            class="custom-tree-node"
            slot-scope="{ data }"
            :title="data.fullName"
          >
            <i :class="data.icon" />
            <span class="text" :title="data.fullName">{{ data.fullName }}</span>
          </span>
        </el-tree>
      </el-scrollbar>
    </div>
  </div>
</template>
<script>
import { getDepartmentSelector } from "@/api/permission/department";
import { getUserList } from "@/api/permission/user";
import { getMenuListByUserId } from "@/api/permission/userSetting";

export default {
  data() {
    return {
      keyword: "",
      treeAllDate: [],
      treeData: [], //菜单树
      tableData: [], //人员列表
      webTreeDate: [],
      appTreeDate: [],
      treeLoading: false,
      listLoading: true,
      listQuery: {
        organizeId: "",
        keyword: "",
        currentPage: 1,
        pageSize: 20,
      },
      defaultProps: {
        children: "children",
        label: "fullName",
      },
      total: 0,
      type: "module", //数据类型  module-菜单 button-按钮  column-列表 resource-数据 form-表单
      user: "",
      menuType: "Web",
      expands: true,
      refreshTree: true,
      filterText: "",
      auditOptions: [
        {
          fullName: "菜单",
          id: "module",
        },
        {
          fullName: "按钮",
          id: "button",
        },
        {
          fullName: "列表",
          id: "column",
        },
        {
          fullName: "数据",
          id: "resource",
        },
        {
          fullName: "表单",
          id: "form",
        },
      ],
      audit: "module",
    };
  },
  watch: {
    filterText(val) {
      this.$refs.treeBox.filter(val);
    },
    menuType(val) {
      this.changType(val);
    },
    audit(val) {
      this.getMenuList(this.user, this.audit);
    },
  },
  created() {
    // this.getMenuList();
  },
  methods: {
    search() {
      this.listQuery.currentPage = 1;
      this.listQuery.pageSize = 20;
      this.listQuery.sort = "desc";
      this.initData();
    },
    reset() {
      this.listQuery.keyword = "";
      this.search();
    },
    //获取菜单信息
    getMenuList(e, type) {
      console.log(e.id);
      this.user = e;
      this.type = type;
      this.treeLoading = true;
      let _query = {
        type: this.type,
        moduleIds: "",
      };
      this.treeData = [];
      this.appTreeDate = [];
      this.webTreeDate = [];
      getMenuListByUserId(this.user.id, _query)
        .then((res) => {
          for (let i = 0; i < res.data.list.length; i++) {
            if (res.data.list[i].fullName == "App菜单") {
              this.appTreeDate.push(res.data.list[i]);
            } else {
              this.webTreeDate.push(res.data.list[i]);
            }
          }
          if ("Web" == this.menuType) {
            this.treeData = this.webTreeDate;
          } else {
            this.treeData = this.appTreeDate;
          }

          this.treeLoading = false;
        })
        .catch(() => {
          this.treeLoading = false;
        });
    },
    changType(val) {
      if ("Web" == this.menuType) {
        this.treeData = this.webTreeDate;
      } else {
        this.treeData = this.appTreeDate;
      }
    },
    filterNode(value, data) {
      if (!value) return true;
      return data.fullName.indexOf(value) !== -1;
    },
    getOrganizeList(isInit) {
      this.treeLoading = true;
      getDepartmentSelector()
        .then((res) => {
          this.treeData = res.data.list;
          this.treeLoading = false;
          if (isInit) this.initData();
        })
        .catch(() => {
          this.treeLoading = false;
        });
    },
    initData() {
      this.treeData = [];
      this.appTreeDate = [];
      this.webTreeDate = [];
      this.listLoading = true;
      let param = {
        ...this.listQuery,
        audit: this.audit,
      };
      getUserList(param)
        .then((res) => {
          this.tableData = res.data.list;
          this.total = res.data.pagination.total;
          this.listLoading = false;
        })
        .catch(() => {
          this.listLoading = false;
        });
    },
    handleNodeClick(data) {
      if (this.listQuery.organizeId === data.id) return;
      this.listQuery.organizeId = data.id;
      this.type = data.type;
      this.reset();
    },
    //左侧树折叠/展开
    toggleExpand(expands) {
      this.refreshTree = false;
      this.expands = expands;
      this.$nextTick(() => {
        this.refreshTree = true;
        this.$nextTick(() => {
          this.$refs.treeBox.setCurrentKey(this.companyId);
        });
      });
    },

    handleDel(id) {
      this.$confirm("此操作将永久删除该用户, 是否继续?", "提示", {
        type: "warning",
      })
        .then(() => {
          delUser(id).then((res) => {
            this.$message({
              type: "success",
              message: res.msg,
              duration: 1500,
              onClose: () => {
                this.initData();
              },
            });
          });
        })
        .catch(() => {});
    },
  },
};
</script>
<style scoped>
>>> .jsbos-el_tabs .el-tabs__content {
  overflow: hidden !important;
}
</style>
